/**
 * Created by Jackie.Chen on 2016/12/4.
 */

var modelList = function () {
    /*基础配置*/
    var el = 'body';
    var addLay = null, updateLay = null;
    var searchObj = {
        modelName: '',
        status: '',
        hangye: '',
        Cj: '',
        pageNum: 1,
        pageSize: 10
    };

    /*操作*/
    function doSearch() {
        var modelName = $("#modelName").val();
        var status = $("#status").attr('data');
        var hangye = $("#hangye").attr('data');
        var Cj = $("#Cj").attr('data');
        searchObj.modelName = modelName || '';
        searchObj.status = status || '';
        searchObj.hangye = hangye || '';
        searchObj.Cj = Cj || '';
        searchObj.pageNum = 1;  //reset
        renderPages(true);
    }

    function renderPages(isFirst) {
        var url = '/modelController/list.do', isAdd = false;
        getHtmlByUrl({
            url: url,
            data: searchObj,
            success: function (res) {
                $("#modelList").html(res);
                if (isFirst) {
                    buildPageArea();
                }
            }
        });
    }

    /*end页面初始化和事件绑定*/


    function getOptionValue(e) {
        var $me = $(this);
        $(this).parents('.dropdown-menu').siblings('.dropdown-toggle').removeClass('error').siblings('.error').remove();
        $me.parents('.dropdown-menu').siblings('.dropdown-toggle').val($me.find('a').text());
        $me.parents('.dropdown-menu').siblings('.dropdown-toggle').attr("data", $me.attr("data"));
    }

    /*分页*/
    function doChangePage(num) {
        searchObj.pageNum = num;
        renderPages();
    }

    function buildPageArea() {
        var pageAllCount = $('#pageAllCount').val(),
            totalPage = Math.ceil(pageAllCount / searchObj.pageSize);
        laypage({
            cont: $('#pageD'),
            pages: totalPage,
            curr: searchObj.pageNum,
            totalCount: pageAllCount,
            skin: '#338af9',
            jump: function (obj, first) {
                if (!first) {
                    doChangePage(obj.curr);
                }
            }
        });
    }

    /*end分页*/
    /*页面初始化和事件绑定*/
    function bindEvents() {
        $(el).undelegate()
            .delegate('.dropdown-menu li', 'click', getOptionValue)
            .delegate('#searchBtn', 'click', doSearch);
            // .delegate('#upAdown', 'click', upAdown);
    }

    /*页面初始化*/
    function init() {
        renderPages(true);
        bindEvents();
    }

    /*end 页面初始化*/
    init();
};
